package com.bhs.sansonglogistics.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bhs.sansonglogistics.BuildConfig;
import com.bhs.sansonglogistics.bean.event.ApplicationStatus;

import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.pgyer.pgyersdk.PgyerSDKManager;
import com.umeng.commonsdk.UMConfigure;

import org.greenrobot.eventbus.EventBus;

import cn.jiguang.api.utils.JCollectionAuth;
import cn.jpush.android.api.JPushInterface;


public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {

    @SuppressLint("StaticFieldLeak")
    private static Context mContext;
    private Activity curActivity;
    private int foregroundActivities = 0;
    private boolean isChangingConfiguration;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        registerActivityLifecycleCallbacks(this);
        initialize();

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        boolean privacyPolicy = SharedPreferencesUtils.getBoolean(this, "privacy", false);
        if (privacyPolicy) {
            new PgyerSDKManager.Init()
                    .setContext(this) //设置上下问对象
                    .start();
        }
    }

    private void initialize() {
        boolean privacyPolicy = SharedPreferencesUtils.getBoolean(this, "privacy", false);
        UMConfigure.preInit(this, "64882f08e56f164659be8245", "logistics");
        UMConfigure.setLogEnabled(BuildConfig.DEBUG);
        new Thread(new Runnable() {
            @Override
            public void run() {
                JPushInterface.setDebugMode(true);
                if (privacyPolicy) {
                    Initialize.init(mContext);
                } else {
                    JCollectionAuth.setAuth(mContext, false); // 后续初始化过程将被拦截
                }
                JPushInterface.init(mContext);
            }
        }).start();
    }

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        curActivity = (Activity) activity;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        curActivity = null;
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        foregroundActivities++;
        if (foregroundActivities == 1 && !isChangingConfiguration) {
            LogUtils.d("前台");
            isChangingConfiguration = false;
            EventBus.getDefault().post(new ApplicationStatus(false));
        }
        isChangingConfiguration = activity.isChangingConfigurations();
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        foregroundActivities--;
        if (foregroundActivities == 0) {
            EventBus.getDefault().post(new ApplicationStatus(true));
        }
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }


}
